import Image from "next/image";
import styles from "./index.module.css";
import { Button, Form, Input, message } from "antd";
import { login } from "@/api/user";
import { useRouter } from "next/router";

export default function Home() {
    const router = useRouter();
    const handleFinish = async (value: { name: string, password: string }) => {
        try {
            const res = await login(value);
            if (res.success) {
                message.success('登陆成功！')
                localStorage.setItem('user', JSON.stringify({ info: res.data, token: res.token }));
                router.push('/')
            } else {
                message.error(res.message ?? '登录失败')
            }
        } catch (error) {
            console.log(error);
        }
    }
    return (
        <div className={styles.container}>
            <h2 className={styles.title}>图书管理系统</h2>
            <Form onFinish={handleFinish}>
                <Form.Item label="账号" name="name" rules={[{ required: true, message: '请输入账号' }]}>
                    <Input placeholder="请输入账号" />
                </Form.Item>
                <Form.Item label="密码" name="password" rules={[{ required: true, message: '请输入密码' }]}>
                    <Input.Password placeholder="请输入密码" />
                </Form.Item>
                <Form.Item>
                    <Button htmlType="submit" className={styles.btn} type="primary" size="large">登陆</Button>
                </Form.Item>
            </Form>
        </div>
    );
}
